CPPS := $(sort $(wildcard *.cpp))
ELFS := $(patsubst %.cpp,out/%.elf,$(CPPS))
TSET := $(patsubst %.cpp,test_%,$(CPPS))

# 列编辑：按住Ctrl+Alt然后鼠标选择

BFLAGS   := -O2 -Wall -Wextra -DLOCAL_TEST=1 -I./include/
CCFLAGS  := $(BFLAGS) -std=c17
CXXFLAGS := $(BFLAGS) -std=c++20 -Weffc++ -Wno-unused-result
LDFLAGS  := -lm -lpthread

ifneq ($(VERBOSE),1)
	QCC    = @echo '  cc    '$<;
	QCXX   = @echo '  c++   '$<;
	QRUN   = @echo '  run   '$<;
	QTEST  = @echo '  test  '$<;
	QCLEAN = @echo '  clean '$<;
endif

all: $(ELFS)

.PHONY: book clean cleanout rebuild test retest

uuid_demo.elf: uuid_demo.cpp
	$(QCXX)$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -l uuid

out/%.elf: %.cpp
	$(QCXX)$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# 用管道直接对比虽然更干净，但保留输出结果便于查错。
test_%: out/%.elf %.in %.ans
	$(QTEST) out//$*.elf < $*.in 2>&1 > $*.tmp && diff $*.ans $*.tmp

test: all $(TSET)

clean: cleanout
	$(QCLEAN) rm -rf out/*.elf out/*.out out/*.o

rebuild: clean all

cleanout:
	$(QCLEAN) rm -rf *.tmp

retest: cleanout test

